package AlienArena::Server;

use 5.006;
use strict;
use warnings;
our $VERSION = '0.01';
use lib '..';

use AlienArena::Server::RunState;
use AlienArena::Server::Properties;
use AlienArena::Server::Player;
use AlienArena::Server::Bot;

use AlienArena::Server::Commands;
use AlienArena::Server::ORM;
use AlienArena::Server::Methods;

# new
# arg 1 : AlienArena::RCon::Console object

sub new {
    my $class = shift;
    my $self = bless {}, $class;
    my $console = shift;
    $self->_console( $console );
    # init Server with appropriate objects
    $self->_run_state( AlienArena::Server::RunState->new($self) );
    $self->_properties( AlienArena::Server::Properties->new($self) );
    $self->_players({});
    $self->_bots({});
    $self->_cvars({});
    return $self;
}

sub console { shift->_console }
sub host { shift->_properties->host }
sub port { shift->_properties->port }
sub pass { shift->_properties->pass }
sub server_name { shift->_properties->name }
sub server_admin { shift->_properties->admin }
sub is_up { shift->_run_state->is_up }

sub get_cvar { 
    my $self = shift;
    my ($cvar_name) = @_;
    return $self->_cvars->{$cvar_name}
}

# cvarlist : return a list of all CVar *objects*

sub cvarlist {
    my $self = shift;
    return values %{$self->_cvars};
}

# dmflags : return the DMFlags CVar object

sub dmflags { shift->get_cvar('dmflags') }

# current_map : the currently running map

sub current_map { shift->_run_state->current_map };

# maplist : return array of map names in the current list

sub maplist { 
    my $self = shift;
    return split( /\s+/, $self->get_cvar('sv_maplist')->value);
}
# private getter/setters

sub _console {
    my $self = shift;
    return $self->{'_console'} = shift if @_;
    return $self->{'_console'};
}

sub _players {
    my $self = shift;
    return $self->{'_players'} = shift if @_;
    return $self->{'_players'};
}

sub _run_state {
    my $self = shift;
    return $self->{'_run_state'} = shift if @_;
    return $self->{'_run_state'};
}

sub _properties {
    my $self = shift;
    return $self->{'_properties'} = shift if @_;
    return $self->{'_properties'};
}

sub _bots {
    my $self = shift;
    return $self->{'_bots'} = shift if @_;
    return $self->{'_bots'};
}

sub _cvars {
    my $self = shift;
    return $self->{'_cvars'} = shift if @_;
    return $self->{'_cvars'};
}

# _add_cvars : args are a AlienArena::Server::CVar

sub _add_cvars {
    my $self = shift;
    my @cvars = @_;
    foreach (@cvars) {
	$self->_cvars->{$_->name} = $_;
    }
}

# _add_players : add to current player list the list in args

sub _add_players {
    my $self = shift;
    my @players = @_;
    foreach (@players) {
	$self->_players->{$_->name} = $_;
    }
}

# _update_player_list : replace current players with the list in args

sub _update_player_list {
    my $self = shift;
    my @players = @_;
    my $playerh = $self->_players;
    for (keys %$playerh) {
	delete $playerh->{$_};
    }
    $self->_add_players(@players);
}

=head1 NAME

AlienArena::Server - Models an Alien Arena server

=head1 SYNOPSIS

    use AlienArena::Server;

    my $foo = AlienArena::Server->new();
    ...

=head1 METHODS

=head2 new

=head2 console

=head2 host

=head2 port

=head2 pass

=head2 server_name

=head2 server_admin

=head2 is_up

=head2 get_cvar($cvar_name)

=head2 cvarlist

=head2 dmflags

=head2 current_map

=head2 maplist

=head1 AUTHOR

Mark A. Jensen, C<< <maj -at- fortinbras -dot- us> >>


=head1 LICENSE AND COPYRIGHT

Copyright 2012 Mark A. Jensen.

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.

=cut

1; # End of AlienArena::Server
